<?php

namespace app\api\controller\User;

use app\services\UserPrizeDrawService;
use plugin\admin\app\model\UserPrizeDraw;
use support\Request;

class PrizeDrawController
{
    public function waitOpenList(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);

            $data = UserPrizeDrawService::waitOpenList($page, $length);
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            echo $e->getMessage() . $e->getTraceAsString();
            return err_rsp($e->getMessage());
        }
    }

    public function prizeDrawList(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);

            $data = UserPrizeDrawService::prizeDrawList($page, $length);
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            echo $e->getMessage() . $e->getTraceAsString();
            return err_rsp($e->getMessage());
        }
    }

    public function winList(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);

            $data = UserPrizeDrawService::winList($page, $length);
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            echo $e->getMessage() . $e->getTraceAsString();
            return err_rsp($e->getMessage());
        }
    }

    public function orderDetail(Request $request)
    {
        try {
            $orderNo = $request->post('order_no');

            $data = UserPrizeDrawService::orderDetail($orderNo);
            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            echo $e->getMessage() . $e->getTraceAsString();
            return err_rsp($e->getMessage());
        }
    }

    public function confirmOrder(Request $request)
    {
        try {
            $orderNo = $request->post('order_no');

            UserPrizeDrawService::confirm($orderNo);
            return ok_rsp('成功');
        } catch (\Exception $e) {
            echo $e->getMessage() . $e->getTraceAsString();
            return err_rsp($e->getMessage());
        }
    }

    public function finish(Request $request)
    {
        try {
            $orderNo = $request->post('order_no');
            $addressId = $request->post('address_id');
            $name = $request->post('name');
            $idcard = $request->post('idcard');

            UserPrizeDrawService::finishReceiveInfo($orderNo, $addressId, [
                'name' => $name,
                'idcard' => $idcard
            ]);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }
}